<template>
  <div class="container">
    <!-- 导航栏 -->
    <van-nav-bar
      title="小智同学"
      left-text="返回"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- 聊天记录 -->
    <div class="chat-list" ref="chat">
      <!-- 每一条聊天 -->
      <div
        class="chat-item"
        v-for="(item, idx) in list"
        :key="idx"
        :class="item.name === 'xz' ? 'left' : 'right'"
      >
        <!-- template:它在逻辑上表示一个整体，但是，在渲染成dom之后， 不会生成一个具体的元素 -->
        <template v-if="item.name === 'xz'">
          <van-image
            fit="cover"
            round
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="chat-pao">{{ item.msg }}</div>
        </template>
        <template v-if="item.name === 'me'">
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="$store.state.photo" />
        </template>
      </div>
    </div>
    <!-- 聊天框 -->
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span @click="send" slot="button" style="font-size:12px;color:#999"
          >提交</span
        >
      </van-field>
    </div>
  </div>
</template>
<script>
// 引入socket.io-client
import io from "socket.io-client";
export default {
  data() {
    return {
      list: [
        {
          name: "xz",
          msg: "你好，我是小智",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "我是编程小王子！",
          timestamp: Date.now()
        },
        {
          name: "xz",
          msg: "你以为会在百度上抄代码，就是程序员了吗？",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "不是这样吗？",
          timestamp: Date.now()
        }
      ],
      word: "", //输入框的值
      socket: null
    };
  },
  methods: {
    send() {
      // 获取输入框的值
      const value = this.word;
      // 判断是否为空
      if (!value) {
        this.$toast("发送内容不能为空");
        return;
      }
      // 向服务端发送消息
      this.socket.emit("message", value);
      // 更新本地，把输入框的值push进list渲染到试图
      this.list.push({
        name: "me",
        msg: value,
        timestamp: Date.now()
      });
      // 清空输入框的值
      this.word = "";
      //scrollHeight文档内容的实际高度，包括溢出的部分
      this.$nextTick(() => {
        this.$refs.chat.scrollTop = this.$refs.chat.scrollHeight;
        // console.log(this.$refs,"nextTick");
      });
    }
  },
  mounted() {
    this.socket = io("http://47.114.163.79:3003", {
      query: {
        token: this.$store.state.tokenInfo.token
      }
    });
    // 创建链接
    this.socket.on("connect", () => {
      console.log("链接成功了");
      this.list.push({
        name: "xz",
        msg: "小智来啦！",
        timestamp: Date.now()
      });
      this.$nextTick(() => {
        this.$refs.chat.scrollTop = this.$refs.chat.scrollHeight;
      });
    });
    // 接受服务器发来的数据
    this.socket.on("message", obj => {
      // console.log(obj);
      this.list.push({
        name: "xz",
        msg: obj.msg,
        timestamp: obj.timestamp
      });
      // 视图更新之后修改scroll卷起的距离，保持每次都是最新的消息正常显示出来
      this.$nextTick(() => {
        this.$refs.chat.scrollTop = this.$refs.chat.scrollHeight;
      });
    });
  }
};
</script>
<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 50px 0 50px 0;
  /deep/.van-nav-bar {
    background-color: #1989fa;
    .van-nav-bar__title {
      color: #ffffff;
    }
    .van-icon {
      color: #ffffff;
      vertical-align: middle;
    }
  }
  .chat-list {
    height: 100%;
    overflow-y: scroll;

    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
